package com.roncoo.education.course.service.admin;

import com.roncoo.education.common.config.ThreadContext;
import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.course.service.admin.biz.AdminExperimentBiz;
import com.roncoo.education.course.service.admin.req.AdminExperimentEditReq;
import com.roncoo.education.course.service.admin.req.AdminExperimentPageReq;
import com.roncoo.education.course.service.admin.req.AdminExperimentSaveReq;
import com.roncoo.education.course.service.admin.resp.AdminExperimentPageResp;
import com.roncoo.education.course.service.admin.resp.AdminExperimentViewResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * ADMIN-实验信息
 *
 * @author wujing
 */
@Api(tags = "ADMIN-实验信息")
@RestController
@RequiredArgsConstructor
@RequestMapping("/course/admin/experiment")
public class AdminExperimentController {

    @NotNull
    private final AdminExperimentBiz biz;

    @ApiOperation(value = "实验信息分页", notes = "实验信息分页")
    @PostMapping(value = "/page")
    public Result<Page<AdminExperimentPageResp>> page(@RequestBody AdminExperimentPageReq req) {
        return biz.page(req, ThreadContext.userId());
    }

    @ApiOperation(value = "实验信息添加", notes = "实验信息添加")
    @PostMapping(value = "/save")
    public Result<String> save(@RequestBody @Valid AdminExperimentSaveReq req) {
        return biz.save(req, ThreadContext.userId());
    }

    @ApiOperation(value = "实验信息查看", notes = "实验信息查看")
    @ApiImplicitParam(name = "id", value = "主键ID", dataTypeClass = Long.class, paramType = "query", required = true)
    @GetMapping(value = "/view")
    public Result<AdminExperimentViewResp> view(@RequestParam Long id) {
        return biz.view(id);
    }

    @ApiOperation(value = "实验信息修改", notes = "实验信息修改")
    @PutMapping(value = "/edit")
    public Result<String> edit(@RequestBody @Valid AdminExperimentEditReq req) {
        return biz.edit(req);
    }

    @ApiOperation(value = "实验信息删除", notes = "实验信息删除")
    @ApiImplicitParam(name = "id", value = "主键ID", dataTypeClass = Long.class, paramType = "query", required = true)
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam Long id) {
        return biz.delete(id);
    }
    
    @ApiOperation(value = "ES同步实验", notes = "将实验同步到ES")
    @GetMapping(value = "/es")
    public Result<String> syncEs() {
    	try {
    		biz.syncEs();
    		return Result.success("实验同步成功");
    	} catch (Exception e) {
			e.printStackTrace();
			return Result.error("实验同步失败： " + e.getMessage());
		}
    }
}
